/**
 * @(#)HomeAdapters.java
 * 
 * @Version: 1
 * @JDK: jdk 1.6.0.XXX
 * @Module: MBA
 */
/*- 				History
 **********************************************
 *  ID      DATE           PERSON       REASON
 *  1     2013-03-04     Yongkai.Yu    Created
 **********************************************
 */


package com.mba.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BlurMaskFilter;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mba.activity.HomeActivity;
import com.mba.activity.R;
import com.mba.bean.ContentInfo;
import com.mba.util.AsyncImageLoader;

/**
 *  主页适配器类
 */
public class HomeAdapters extends BaseAdapter {
    
    /**
     * 为提高效率，缓存数据准备的一个自定义类 对应一条微博数据
     */
    class ContentHolder {
        public ImageView content_image; // 对应微博显示的图片
        public ImageView content_icon;  // 对应发微博人的头像
        public TextView content_user;   // 对应发微博人的名称
        public TextView content_time;   // 对应发微博的时间
        public TextView content_text;   // 对应发微博的内容
        public TextView content_area;   // 对应发微博的内容
        public TextView content_reply; 
    }
    
    private  HomeActivity homeActivity = null;
    /**
     * 数据集
     */
    public ArrayList<ContentInfo> contentList = null;
    /**
     * @param homeActivity
     * @param contentList 
     */
    public HomeAdapters(HomeActivity homeActivity, ArrayList<ContentInfo> contentList) {
        this.homeActivity = homeActivity;
        this.contentList = contentList;
    }
    
    //微博图片的异步下载类
    AsyncImageLoader asyncImageLoader;
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        asyncImageLoader = new AsyncImageLoader();
        //
        convertView = LayoutInflater.from(this.homeActivity.getApplicationContext()).inflate(R.layout.timeline_item, null);
        //创建一个层次对应组件的类
        ContentHolder ch = new ContentHolder();
        //
  
        //ch.content_icon = (ImageView) convertView.findViewById(R.id.content_head);
        ch.content_area = (TextView) convertView.findViewById(R.id.timeline_item_city_and_area);
        ch.content_area.setBackgroundResource(R.color.lake_bule);
        ch.content_time = (TextView) convertView.findViewById(R.id.timeline_item_created_at);
        //ch.content_image = (ImageView) convertView.findViewById(R.id.content_image);
        ch.content_text = (TextView) convertView.findViewById(R.id.timeline_item_user_and_content);
        ch.content_reply = (TextView) convertView.findViewById(R.id.timeline_item_answer_count);
        ch.content_user = (TextView) convertView.findViewById(R.id.timeline_item_username);
        
        
        //以下为评论页面
        LinearLayout localObject = (LinearLayout)convertView.findViewById(R.id.timeline_item_ll_answers);
        //convertView.findViewById(R.id.timeline_item_td).setVisibility(View.VISIBLE);
        localObject.removeAllViews();
        localObject.setVisibility(View.VISIBLE);
        
        
        
        //获得一条微博数据
        ContentInfo info = this.contentList.get(position);
        if (info != null) {
            convertView.setTag(info.getId());
            ch.content_area.setText("广州天河区");
            ch.content_time.setText(info.getTime());
            ch.content_text.setText(info.getText(), TextView.BufferType.SPANNABLE);
            ch.content_reply.setText("15");
            ch.content_user.setText("掌柜");
            
          
            for(int i=0; i<2;i++){
            
            //评论的layout
            LinearLayout localLinearLayout = new LinearLayout(this.homeActivity);
            //第一个参数为宽的设置，第二个参数为高的设置。 
			LinearLayout.LayoutParams localLayoutParams = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.FILL_PARENT,
					LinearLayout.LayoutParams.FILL_PARENT);
			localLayoutParams.setMargins(0, 0, 5, 0);
	        localLinearLayout.setLayoutParams(localLayoutParams);
	        localLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
            
	        //评论人头像
	        ImageButton localImageButton = new ImageButton(this.homeActivity);
	        
	        //头像尺寸
	        int dimens = 50;
	        int scale  = (int) this.homeActivity.getResources().getDisplayMetrics().density;
	        int i2 = (int)(dimens * scale );
	        LinearLayout.LayoutParams localLayoutParams2 = new LinearLayout.LayoutParams(i2, i2);
			
	        localLayoutParams2.setMargins(0, 0, 10, 0);
	        localImageButton.setPadding(0, 0, 0, 0);
	        localImageButton.setLayoutParams(localLayoutParams2);
	        Drawable localDrawable =  this.homeActivity.getResources().getDrawable(R.drawable.default_avatar);
	        localImageButton.setBackgroundDrawable(localDrawable);
	        ImageView.ScaleType localScaleType = ImageView.ScaleType.CENTER_CROP;
	        localImageButton.setScaleType(localScaleType);
	        /*
	        Bitmap localBitmap = (Bitmap);
	        localImageButton.setImageBitmap(localBitmap);
	        */
	        localImageButton.setFocusable(true);
	        
	        //评论内容
	        TextView localTextView = new TextView(this.homeActivity);
	        
			LinearLayout.LayoutParams localLayoutParams3 = new LinearLayout.LayoutParams(
					LinearLayout.LayoutParams.FILL_PARENT,
					LinearLayout.LayoutParams.FILL_PARENT);
			
	        localTextView.setLayoutParams(localLayoutParams3);
	        //localTextView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 1096810496);
	        int i3 = (int) this.homeActivity.getResources().getDisplayMetrics().density;
	        float f = MeasureSpec.EXACTLY * i3;
	        //localTextView.setLineSpacing(f, 1065353216);
	        StringBuilder localStringBuilder4 = new StringBuilder("<font color='#8c8c8c'>");
	        String str10 = "红尘小二";
	        StringBuilder localStringBuilder5 = localStringBuilder4.append(str10).append(": </font>").append("<font color='#282828'>");
	        String str11 = "好主意，我有兴趣~";
	        localStringBuilder5.append(str11);
	        Spanned localSpanned2 = Html.fromHtml(localStringBuilder5.toString() + "</font>");
	        localTextView.setText(localSpanned2);
	        
	        localLinearLayout.addView(localImageButton);
	        localLinearLayout.addView(localTextView);
	        ((LinearLayout)localObject).addView(localLinearLayout);
	        
	        //分割线
	       
	          ImageView localImageView = new ImageView(this.homeActivity);
	          LinearLayout.LayoutParams localLayoutParams4 = new LinearLayout.LayoutParams(-1, 1);
	          int i5 = (int) this.homeActivity.getResources().getDisplayMetrics().density;
	          int i6 = (int)(10 * i5 + 10);
	          int i7 = (int) this.homeActivity.getResources().getDisplayMetrics().density;
	          int i8 = (int)(10 * i7 + 10);
	          localLayoutParams4.setMargins(0, i6, 0, i8);
	          localImageView.setLayoutParams(localLayoutParams4);
	          int i9 = this.homeActivity.getResources().getColor(R.color.white);
	          localImageView.setBackgroundColor(i9);
	          ((LinearLayout)localObject).addView(localImageView);
	          ((LinearLayout)localObject).addView(localImageView);
	          ((LinearLayout)localObject).addView(localImageView);
	    	
            }
          
        }
        return convertView;
    }

    @Override
    public int getCount() {
        return this.contentList.size();
    }

    @Override
    public Object getItem(int position) {
        return this.contentList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    
    /**
     * 设置LinearLayout的背景。用xml文件画Rectangle来作为LinearLayout的背景
     * 通过.xml创建一个Drawable，然后制定宽和高， 将Drawable转化bitmap画阴影bitmap，
     * 然后将阴影bitmap转化为drawable，最后将drawable设置为LinearLayout的背景。
     * @return
     */
    /*
    private  Bitmap drawableToBitmap(Drawable drawable,int width,int height) {          
        Bitmap bitmap = Bitmap.createBitmap(width,height,  
        drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888: Bitmap.Config.RGB_565);  
        Canvas canvas = new Canvas(bitmap);  
        drawable.setBounds(0, 0, width, height);  
        drawable.draw(canvas);  
        return bitmap;          
    } 
    
    public void drawLayoutDropShadow(Drawable drawable,View v, int width , int height){
    	BlurMaskFilter blurFilter = new BlurMaskFilter(3, BlurMaskFilter.Blur.OUTER);
 		Paint shadowPaint = new Paint();		
 		shadowPaint.setMaskFilter(blurFilter);
 		//Drawable drawable = c.getResources().getDrawable(R.drawable.try_shadows);
 		int[] offsetXY =new int[2]; 		
 		Bitmap originalBitmap = drawableToBitmap(drawable,width,height);
 		Bitmap shadowBitmap = originalBitmap.extractAlpha(shadowPaint,offsetXY);
 		Bitmap shadowImage32 = shadowBitmap.copy(Bitmap.Config.ARGB_8888, true);
 		
 		Canvas c = new Canvas(shadowImage32);	
 		c.drawBitmap(originalBitmap, 0,0, null);
 		
 		Drawable d =new BitmapDrawable(shadowImage32);
 		v.setBackgroundDrawable(d);
 	//	linearLayout.setBackgroundDrawable(d);
    }
   */ 
}